import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List _imageUrls = [
    'http://pages.ctrip.com/commerce/promote/20180718/yxzy/img/640sygd.jpg',
    'https://dimg04.c-ctrip.com/images/700u0r000000gxvb93E54_810_235_85.jpg',
    'https://dimg04.c-ctrip.com/images/700c10000000pdili7D8B_780_235_57.jpg'
  ];
  int get _imageUrlsLen {
    return _imageUrls.length;
  }

  Swiper _swiper() {
    return Swiper(
      itemCount: _imageUrlsLen,
      autoplay: true,
      itemBuilder: (BuildContext context, int index) {
        return Image.network(_imageUrls[index], fit: BoxFit.fill);
      },
      pagination: SwiperPagination(),
    );
  }

  static const double APPBAR_SCROLL_OFFSET = 100;
  double appBarAlpha = 0;

  _onScroll(offset) {
    print(offset);
    double alpha = offset / APPBAR_SCROLL_OFFSET;
    if (alpha < 0) {
      alpha = 0;
    } else if (alpha > 1) {
      alpha = 1;
    }
    setState(() {
      appBarAlpha = alpha;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        // appBar: AppBar(title: Text('首页')),
        body: Stack(
      children: [
        MediaQuery.removePadding(
            removeTop: true,
            context: context,
            child: NotificationListener(
                onNotification: (scrollNotification) {
                  if (scrollNotification is ScrollUpdateNotification &&
                      scrollNotification.depth == 0) {
                    //滚动且是列表滚动的时候 depth==0 第0个元素
                    _onScroll(scrollNotification.metrics.pixels);
                  }
                  return true;
                },
                child: ListView(
                  children: [
                    Container(
                      height: 160,
                      child: _swiper(),
                    ),
                    Container(
                        height: 800,
                        child: Center(
                            child: TextButton(
                              onPressed: null,
                                child: Text('首页'))))
                  ],
                ))),
        Opacity(
            opacity: appBarAlpha,
            child: Container(
                height: 80,
                decoration: BoxDecoration(color: Colors.white),
                child: Center(
                    child: Padding(
                        padding: EdgeInsets.only(top: 20), child: Text('首页')))))
      ],
    ));
  }
}
